<?php
class admin {
	var $plist = array ();
	var $showplist = '';
	var $lsjf = 0;
	var $showdailog = false;
	var $dailogcontent = '';
	function doAction($action, $posti) {
		global $db;
		global $model;
		switch ($action) {
			case 'changepubset' :
				$tab = 1;
				$db->ExeQuerySql ( "SELECT * FROM `cu_public` WHERE id=1" );
				if ($db->FetchRow ()) {
					$db->ExeSql ( "update `cu_public` set nbsusername='$posti[nbsusername]',nbspassword='$posti[nbspassword]',notictext='$posti[notictext]' where id=1" );
				}
				break;
			case 'searchsmj' :
				$model = true;
				self::searchsmj ();
				break;
			case 'usraddsubmit' :
				$model = true;
				self::addusr ( $posti );
				break;
			case 'nocaddsubmit' :
				$model = true;
				self::addnoc ( $posti );
				break;
			case 'log' :
				$db->ExeQuerySql ( "select * from `cu_manage` where username='$posti[adminname]' and password='$posti[adminpassword]'" );
				if ($db->FetchRow ()) {
					$root = $db->Get ( "root" );
					if ($root >= 3) {
						$_SESSION ['uid'] = $posti ['adminname'];
						$_SESSION ['root'] = $root;
					} else {
						echo "<script>alert('对不起，你還沒有資格進入!');</script>";
					}
				} else {
					echo "<script>alert('对不起，用戶名或密碼錯!');</script>";
				}
				break;
			case 'plusjf' :
				$tab = 2;
				if ($posti ['plusjfreason'] == '') {
					echo "<script>alert('对不起，請填寫加分理由!');</script>";
				} else {
					$JF->updatalinshijifen ( $posti ['plusjfusr'], $posti ['plusjfreason'] );
				}
				$this->updatalist ( null );
				break;
		}
	}
	function doAcav($action, $value) {
		global $model;
		switch ($action) {
			case 'pass':
				/* 彈出對話框,格式為getpassdailog(用戶名,操後停留的版塊) */
				$this->getpassdailog ( $value, 4 );
				break;
			case 'passuser' :
				$this->passuser ( $value );
				$this->updatalist ( null );
				break;
			case 'closedailog':
				/* 關閉對話框 */
				$tab = $value;
				$this->showdailog = false;
				break;
			case 'ctrlroot' :
				$this->getctrlrootdailog ( $value );
				break;
			case 'chekusergroup' :
				$this->updatausergrouplist ();
				break;
			case 'logout' :
				unset ( $_SESSION ['uid'] );
				break;
			case 'mysys' :
				$model = true;
				include './dtpl/mysys.dpl';
				break;
			case 'usrg' :
				$model = true;
				include './dtpl/usrg.dpl';
				break;
				break;
			case 'usradd' :
				$model = true;
				include './dtpl/usradd.dpl';
				break;
			case 'usrsut' :
				$model = true;
				include './dtpl/usrsut.dpl';
				break;
			case 'tongji' :
				$model = true;
				include './dtpl/tongji.dpl';
				break;
			case 'zengzhi' :
				$model = true;
				include './dtpl/zengzhi.dpl';
				break;
			case 'ctj' :
				$model = true;
				switch ($value) {
					case 'zrf' :
						include './dtpl/zrf.dpl';
						break;
					case 'tt' :
						include './dtpl/tt.dpl';
						break;
					case 'smj' :
						include './dtpl/smj.dpl';
						break;
					case 'tsj' :
						include './dtpl/tsj.dpl';
						break;
					case 'zj' :
						include './dtpl/zj.dpl';
						break;
					default :
						echo "请选择正确的查询分类才能加载组件！";
						break;
				}
				break;
			case 'zzyw' :
				$model = true;
				switch ($value) {
					case 'espt' :
						include './dtpl/espt.dpl';
						break;
					case 'smj' :
						include './dtpl/smj.dpl';
						break;
					default :
						echo "请选择正确的查询分类才能加载组件！";
						break;
				}
				break;
			case 'searchtt' :
				$model = true;
				self::searchtt ();
				break;
			case 'searchsmj' :
				$model = true;
				if (! empty ( $_POST )) {
					self::searchsmj ( $_POST );
				} else {
					self::searchsmj ();
				}
				break;
			case 'searchtsj' :
				$model = true;
				if (! empty ( $_POST )) {
					self::searchtsj ( $_POST );
				} else {
					self::searchtsj ();
				}
				break;
			case 'searchzrf' :
				$model = true;
				self::searchzrf ( $_POST );
				break;
			case 'searchzj' :
				$model = true;
				if (! empty ( $_GET ['stimeb'] )) {
					$posti = array (
							$_GET ['stimeb'],
							$_GET ['stimee'] 
					);
					self::searchzj ( $posti );
				} else {
					self::searchzj ();
				}
				break;
			case 'resetnoc' :
				$model = true;
				self::resetnoc ();
				break;
			case 'resetthj' :
				$model = true;
				self::resetthj ();
				break;
			case 'listnoc' :
				$model = true;
				self::listnoc ();
				break;
			case 'resetnocadd' :
				$model = true;
				include './dtpl/nocadd.dpl';
				break;
		}
	}
	public static function listnoc($posti = NULL) {
		global $db;
		$items = array ();
		$dmt = self::getdmt ();
		$db->ExeQuerySql ( "select * from cu_noc where time >= '$dmt[dmtb]' and time <= '$dmt[dmte]'" );
		if (! empty ( $posti )) {
			$db->ExeQuerySql ( "select * from cu_qcxq where time >= '$posti[stimeb]' && time <= '$posti[stimee]'" );
		}
		while ( $row = $db->GetFetchArray () ) {
			array_push ( $items, $row );
		}
		$value = array (
				"total" => 28,
				'rows' => $items 
		);
		$nonetr = json_encode ( $value );
		echo $nonetr;
	}
	private static function resetnoc() {
		global $db;
		try {
			$sql = "CREATE TABLE `cu_noc` (
`snid`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`cname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`cnumber`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`overtime`  enum('是','否') NULL ,
`isduban`  enum('是','否') NULL ,
`fanying`  longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`huidan`  longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`ctlman`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`gdlb`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`bz`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`time`  datetime NULL
)
;";
			$db->ExeSql ( $sql );
			echo '成功';
		} catch ( Exception $e ) {
			print $e->getMessage ();
		}
	}
	private static function resetthj() {
		global $db;
		try {
			$sql = "CREATE TABLE `cu_thj` (
`snid`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`cname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`cnumber`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`type`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`orderdate`  datetime NULL ,
`ctlman`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`ctldate`  datetime NULL ,
`shixian`  enum('超过15天','7至15天内','7天内') CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`liyou`  enum('上级要求','政府要求','强烈要求','差价补换','异价异款','同价异款','销售误导','产品质量') CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`fanying`  longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`jieshi`  longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`o_cellphone`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`o_sn`  varchar(255) NULL ,
`n_cellphone`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`n_sn`  varchar(255) NULL ,
`o_cn`  longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`n_cn`  longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`bz`  longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`jikuaidi`  varchar(255) NULL ,
`shoukuaidi`  varchar(255) NULL ,
`chajia`  varchar(255) NULL ,
`tuikuan`  varchar(255) NULL ,
`o_address`  longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
`n_address`  longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
)
;";
			$db->ExeSql ( $sql );
			echo '成功';
		} catch ( Exception $e ) {
			print $e->getMessage ();
		}
	}
	private static function getdmt() {
		$y = date ( "Y", time () );
		$m = date ( "m", time () );
		$d = date ( "d", time () );
		$t0 = date ( 't' ); // 本月一共有几天
		$t1 = mktime ( 0, 0, 0, $m, 1, $y ); // 创建本月开始时间
		$t2 = mktime ( 23, 59, 59, $m, $t0, $y ); // 创建本月结束时间
		$rdate ['today'] = date ( "Y-m-d", time () );
		$rdate ['dmtb'] = date ( "Y-m-d H:i:s", $t1 );
		$rdate ['dmte'] = date ( "Y-m-d H:i:s", $t2 );
		$rdate ['sjc'] = $t2 - $t1;
		return $rdate;
	}
	private static function formattime($stt, $ett) {
		$formated ['stt'] = date ( 'Y-m-d H:i:s', strtotime ( $stt ) );
		$formated ['ett'] = date ( 'Y-m-d H:i:s', strtotime ( $ett ) );
		return $formated;
	}
	public static function searchzj($posti = NULL) {
		global $db;
		$items = array ();
		$dmt = self::getdmt ();
		$db->ExeQuerySql ( "select * from cu_qcxq where time >= '$dmt[dmtb]' and time <= '$dmt[dmte]'" );
		if (! empty ( $posti )) {
			$db->ExeQuerySql ( "select * from cu_qcxq where time >= '$posti[stimeb]' && time <= '$posti[stimee]'" );
		}
		while ( $row = $db->GetFetchArray () ) {
			array_push ( $items, $row );
		}
		$value = array (
				"total" => count ( $items ),
				'rows' => $items 
		);
		$nonetr = json_encode ( $value );
		echo $nonetr;
	}
	public static function searchzrf($posti = NULL) {
		global $db;
		$trname = false;//識別是否需要修正字段名
		$items = array ();
		if (! empty ( $posti )) {
			$stf = self::formattime($posti['stimeb'],$posti['stimee']);
			switch ($posti ['cctype']) {
				case 'noc' :
					$sql = "select * from cu_noc where time >= '$stf[stt]' && time <= '$stf[ett]' && xsygs='是'";
					$trname = true;//由於數據字段不同，需在輸出json之前修改好。
					break;
				case 'glxq' :
					$sql = "select * from cu_glxq where time >= '$stf[stt]' && time <= '$stf[ett]' && xsygs='是'";
					break;
				default :
					$sql = "select * from cu_glxq where time >= '$stf[stt]' && time <= '$stf[ett]' && xsygs='是'";
					break;
			}
			$db->ExeQuerySql ( $sql );
			while ( $row = $db->GetFetchArray () ) {
				if ($trname) {
					$row['submitnumber']=$row['snid'];
					$row['contactnumber']=$row['cnumber'];
					$row['endtime']=$row['time'];
					$row['customername']=$row['cname'];
					$row['isend']=$row['gdlb'];
				}
				array_push ( $items, $row );
			}
		} else {
			$row = array (
					'submitnumber' => '请按条件查询！' 
			);
			array_push ( $items, $row );
		}
		$value = array (
				"total" => count ( $items ),
				'rows' => $items 
		);
		$nonetr = json_encode ( $value );
		echo $nonetr;
	}
	public static function searchtt() {
		try {
			global $db;
			$smjjson = fopen ( "./json/datagrid_smj.json", "w" );
			// 统计数据
			$db->ExeQuerySql ( "select * from cu_glxq" );
			$row ['ptzgdl'] = $db->GetResultCount ();
			$row ['szgds'] = $db->GetCount ( "cu_glxq", "isend", "归档" );
			$row ['smzgds'] = $db->GetCount ( "cu_glxq", "gdlbvalue", "激活卡申请" );
			$row ['hggds'] = $db->GetCount ( "cu_glxq", "gdqc", "是" );
			$row ['gdhgl'] = ($row ['hggds'] / $row ['szgds']) * 100;
			// 生成json并放置到json文件中
			$items = array ();
			array_push ( $items, $row );
			$value = array (
					"total" => 1,
					'rows' => $items 
			);
			$nonetr = json_encode ( $value );
			fwrite ( $smjjson, $nonetr );
			fclose ( $smjjson );
			echo $nonetr;
		} catch ( Exception $e ) {
			print $e->getMessage ();
		}
	}
	public static function searchsmj($posti = NULL) {
		global $db;
		$items = array ();
		$dmt = self::getdmt ();
		$db->ExeQuerySql ( "select * from cu_glxq where gdlbvalue = '激活卡申请' and time >= '$dmt[dmtb]' and time <= '$dmt[dmte]'" );
		if (! empty ( $posti )) {
			$db->ExeQuerySql ( "select * from cu_glxq where gdlbvalue = '激活卡申请' && time >= '$posti[stimeb]' && time <= '$posti[stimee]'" );
		}
		while ( $row = $db->GetFetchArray () ) {
			array_push ( $items, $row );
		}
		$value = array (
				"total" => count ( $items ),
				'rows' => $items 
		);
		$nonetr = json_encode ( $value );
		echo $nonetr;
	}
	public static function searchtsj($posti = NULL) {
		global $db;
		$items = array ();
		$dmt = self::getdmt ();
		$db->ExeQuerySql ( "select * from cu_glxq where gdlbvalue != '激活卡申请' and time >= '$dmt[dmtb]' and time <= '$dmt[dmte]'" );
		if (! empty ( $posti )) {
			$db->ExeQuerySql ( "select * from cu_glxq where gdlbvalue != '激活卡申请' && time >= '$posti[stimeb]' && time <= '$posti[stimee]'" );
		}
		while ( $row = $db->GetFetchArray () ) {
			array_push ( $items, $row );
		}
		$value = array (
				"total" => count ( $items ),
				'rows' => $items 
		);
		$nonetr = json_encode ( $value );
		echo $nonetr;
	}
	public static function addusr($posti) {
		global $db;
		$sql = "insert into cu_manage (nickname,username,password,root,lastlogin,jifen,qqnumber,tuijianren,jointime) values('$posti[name]','$posti[eid]','$posti[pwd]','$posti[wg]',now(),-1,'$posti[cn]','$posti[mng]',now())";
		try {
			$db->ExeSql ( $sql );
			$db->ExeQuerySql ( "select nickname from cu_manage where username='$posti[eid]'" );
			if ($db->FetchRow ()) {
				echo "系统已成功添加用户！";
			}
		} catch ( Exception $e ) {
			if ($e->g == "") {
				print $e->getMessage ();
			}
		}
		$db->FreeResult ();
	}
	public static function addnoc($posti) {
		global $db;
		$sql = "INSERT INTO `cu_noc` (`snid`, `cname`, `cnumber`, `overtime`, `isduban`, `fanying`, `huidan`, `gdlb`, `bz`, `time`) VALUES ('$posti[snid]', '$posti[cname]', '$posti[cnumber]', '$posti[overtime]', '$posti[isduban]', '$posti[fanying]', '$posti[huidan]', '$posti[gdlb]', '$posti[bz]', now());";
		try {
			$db->ExeSql ( $sql );
			$db->ExeQuerySql ( "select snid from cu_noc where snid='$posti[snid]'" );
			if ($db->FetchRow ()) {
				echo "系统已成功添加纪录！<a href=\"javascript:void(0)\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-add'\" onclick=\"resetnoc_add()\">返回继续新增工单</a>";
			}
		} catch ( Exception $e ) {
			if ($e->g == "") {
				print $e->getMessage ();
			}
		}
		$db->FreeResult ();
	}
	public static function getusrglist() {
		global $db;
		$showoptions = '';
		$db->ExeQuerySql ( "select * from cu_usergroup" );
		while ( $row = $db->GetFetchArray () ) {
			$showoptions .= '<option value="' . $row ['number'] . '">' . $row ['name'] . '</option>';
		}
		return $showoptions;
	}
	public static function show_ttl() {
		global $db;
		// $db->ExeQuerySql("select * from cu_gongdan");
		$ttc = $db->GetResultCount ();
		$tttd = '<td>' . $ttc . '</td><td></td><td></td><td></td><td></td><td></td>';
		return $tttd;
	}
	function getctrlrootdailog($username = "") {
		$page = '<div class="publicdailog">
		<div class="dailogtittle">修改用戶權限：</div>
		<div id=""><form><table id="ctrlroot"><tr><td><input name="page1root" type="checkbox" value="" /></td><td>允許查看知識庫</td><td><input name="page2root" type="checkbox" value="" /></td><td>允許下工單</td><td><input name="page3root" type="checkbox" value="" /></td><td>允許查看工單系統</td><td><input name="page4root" type="checkbox" value="" /></td><td>允許查看激活卡頁面</td></tr><tr><td><input name="page5root" type="checkbox" value="" /></td><td>允許使用質檢系統</td><td><input name="page6root" type="checkbox" value="" /></td><td>允許查看知识库采编頁面</td><td></td><td>允許快速查看未領取工單</td><td></td><td>允許快速查看待辦工單</td><td></td><td></td></tr></table><input name="saveroot" type="submit" value="保存" /><input name="closedailog" type="button" value="取消" onclick="javascript:window.location.href=\'index.php?action=closedailog\'"/></form></div>
		</div>';
		$this->showdailog = true;
		$this->dailogcontent = $page;
	}
	function is_adminlog() {
		if (empty ( $_SESSION ['uid'] )) {
			$is_user = false;
		} else {
			if ($_SESSION ['root'] >= 2) {
				$is_user = true;
			} else {
				$is_user = false;
			}
		}
		return $is_user;
	}
	function passuser($user) {
		global $db;
		$db->ExeQuerySql ( "SELECT * FROM `cu_manage` WHERE username='$user'" );
		if ($db->FetchRow ()) {
			$db->ExeSql ( "update `cu_manage` set root='1' where username='$user'" );
			echo "<script>alert('操作成功!');</script>";
		}
	}
	public static function updatalist() {
		global $db;
		$db->ExeQuerySql ( "SELECT * FROM `cu_manage`" );
		$showplist = '';
		while ( $row = $db->GetFetchArray () ) {
			$showplist .= '<tr><td>' . $row ['nickname'] . '</td><td>' . $row ['username'] . '</td><td>' . $row ['root'] . '</td><td>' . $row ['qqnumber'] . '</td><td>' . $row ['lastlogin'] . '</td><td>' . self::getroottool ( $row ['root'], $row ['username'] ) . '</td></tr>';
		}
		return $showplist;
	}
	public static function updatausergrouplist() {
		global $db;
		$db->ExeQuerySql ( "SELECT * FROM `cu_usergroup`" );
		$showplist = '';
		while ( $row = $db->GetFetchArray () ) {
			$showplist .= '<tr><td>' . $row ['number'] . '</td><td>' . $row ['name'] . '</td><td>' . $row ['page1'] . '</td><td>' . $row ['page2'] . '</td><td>' . $row ['page3'] . '</td><td>' . $row ['page4'] . '</td><td>' . $row ['page5'] . '</td><td>' . $row ['page6'] . '</td><td>' . $row ['chekwaitorder'] . '</td><td>' . $row ['chekngorder'] . '</td><td>' . $row ['gdcl'] . '</td><td>' . $row ['gdsc'] . '</td><td><a href="index.php?action=ctrlroot&value=' . $row ['number'] . '">修改</a></td></tr>';
		}
		return $showplist;
	}
	public static function getroottool($root, $username) {
		$adminroot = $_SESSION ['root'];
		$tgyz = '<a href="index.php?action=pass&value=' . $username . '" class="easyui-linkbutton" data-options="iconCls:\'icon-save\'">認證該用戶</a>';
		$scyh = '<a href="#" class="easyui-linkbutton" data-options="iconCls:\'icon-remove\'">删除</a>';
		return $tgyz . '|' . $scyh;
	}
	function getpassdailog($username, $tab) {
		$page = '<div id="dailog">
		<div id="dailogtittle">警告：</div>
		<div id="dailogmain">此操作將會允許該人進入我們的系統，是否繼續？<br><table id="tof" border="0" cellspacing="0"><tr class="dailog_btn">
		<td class="dailog_yes_or_no"><a href="index.php?action=passuser&value=' . $username . '">是</a></td><td class="dailog_yes_or_no"><a href="index.php?action=closedailog&value=' . $tab . '">否</a></td>
		</tr></table></div>
		</div>';
		$this->showdailog = true;
		$this->dailogcontent = $page;
	}
	function getloginpage() {
		$page = '<div id="w" class="easyui-window" title="后台登陆" data-options="iconCls:\'icon-save\',minimizable:false," style="width:500px;height:200px;padding:10px;">
        <form id="jfdl" name="jfdl" method="post" action="index.php?action=log">
            <table id="adminlogtab" border="0" cellspacing="0"><tr height="20"><td>賬戶:<input type="text" name="adminname" id="an"/></td><td rowspan="2"><input id="submitlog" name="adminlog-submit" value="登錄 " type="submit" /></td></tr><tr><td>密碼：<input type="password" name="adminpassword" id="ap"/></td></tr></table>
            </form>
    </div><div id="adminlogba">';
		return $page;
	}
}